<template>
	<view class="AddressManagement">
		<view v-if="List.length != 0">
			<view @click="addresschoose(item)" v-for="(item , index) in List" class="AddressManagement_list">
				<view class="AddressManagement_list_top">
					<view class="AddressManagement_list_top_left">
						<view class="AddressManagement_list_top_left_title">
							{{item.name}}
						</view>
						<view class="AddressManagement_list_top_left_iphone">
							{{ item.phone}}
						</view>
						<view v-if="item.areDefault" class="AddressManagement_list_top_left_default">
							默认
						</view>
					</view>
					<view class="AddressManagement_list_top_right">
						<image @click.stop="editLink(item.id)" class="edit" src="../../static/mine/addressEdit.png"
							mode="widthFix">
						</image>
						<image @click.stop="deleteFun(item.id , item.name)" class="delete"
							src="../../static/mine/addressDelete.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="AddressManagement_list_bottom">
					{{item.allAddressInfo}}
				</view>
			</view>
			<u-loadmore @loadmore="addmodle" :status="status" />
		</view>
		<view v-if="List.length == 0">
			<u-empty text="地址为空" mode="car" icon="/static/home/list.png">
			</u-empty>
		</view>
		<button @click="addLink" class="AddressManagement_button">
			添加收货地址
		</button>

		<u-modal :show="show" @confirm="confirm" @cancel="show = false" ref="uModal" @close="close" showCancelButton
			:closeOnClickOverlay="true">
			是否删除名为“{{deleteName}}”的地址吗？
		</u-modal>
	</view>
</template>

<script>
	import {
		bootreceivingAddressselectpage,
		bootreceivingAddressdelete
	} from '@/api/home'
	export default {
		data() {
			return {
				deleteName: '',
				deleteID: '',
				show: false,
				status: "loadmore",
				queryInquire: {
					pageSize: 10,
					pageNum: 1,
				},
				loding: false,
				List: [],
				over: false,
				type: 0
			}
		},
		onLoad(res) {
			if (res.type) {
				this.type = res.type
			}
			// this.bootreceivingAddressselectpage()
		},
		onReachBottom() {
			this.addmodle();
		},
		onShow() {
			this.queryInquire.pageNum = 1
			this.List = []
			this.over = false
			this.status = 'loadmore'
			this.addmodle();
		},
		methods: {
			addresschoose(row) {
				if (this.type == 2) {
					uni.setStorageSync('addresschoose', JSON.stringify(row));
					uni.navigateBack();
				}
			},
			close() {
				this.show = false
			},
			confirm() {
				bootreceivingAddressdelete(this.deleteID).then(res => {
					uni.showToast({
						title: res.message,
						duration: 2000,
						icon: 'none'
					});
					if (res.success == 1) {
						this.show = false
						this.queryInquire.pageNum = 1
						this.List = []
						this.over = false
						this.status = 'loadmore'
						this.addmodle();
					}
				})
			},
			deleteFun(id, name) {
				this.deleteID = id;
				this.deleteName = name;
				this.show = true
			},
			addmodle() {
				if (this.over == true) {
					return;
				}
				if (this.status == "loadmore") {
					this.bootreceivingAddressselectpage();
				}
			},
			bootreceivingAddressselectpage() {
				this.status = "loading";
				bootreceivingAddressselectpage(this.queryInquire).then((res) => {
						if (res.success == 1) {
							this.List = this.List.concat(res.data.records);
							if (res.data.current == res.data.pages) {
								this.over = true;
								setTimeout(() => {
									this.status = "nomore";
								}, 0);
							} else {
								this.queryInquire.pageNum++;
							}
						}
					})
					.finally(() => {
						this.status = "loadmore";
					});
			},
			editLink(id) {
				uni.navigateTo({
					url: '/pages/mine/addressEdit?id=' + id,
				});
			},
			addLink() {
				uni.navigateTo({
					url: '/pages/mine/addressAdd',
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #E4E6E9;
	}

	.AddressManagement {
		padding: 20upx 58upx;
		padding-bottom: calc(58upx + 82upx);

		.AddressManagement_list {
			padding: 20upx 0;
			border-bottom: 1upx solid rgba(151, 151, 151, 0.2);

			.AddressManagement_list_top {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.AddressManagement_list_top_left {
					display: flex;
					align-items: flex-end;
					position: relative;

					.AddressManagement_list_top_left_title {
						width: 140upx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						font-size: 34upx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: rgba(0, 0, 0, 0.8);
						line-height: 48upx;
					}

					.AddressManagement_list_top_left_iphone {
						font-size: 28upx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: rgba(0, 0, 0, 0.54);
						margin-left: 20upx;
						line-height: 40upx;
					}

					.AddressManagement_list_top_left_default {
						position: absolute;
						left: calc(100% + 10upx);
						top: 0;
						width: 60upx;
						height: 32upx;
						background: #FFBE53;
						border-radius: 8upx;
						text-align: center;
						font-size: 22upx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #000000;
						line-height: 32upx;
					}
				}

				.AddressManagement_list_top_right {
					display: flex;
					align-items: center;

					.edit {
						width: 46upx;
						margin-right: 54upx;

					}

					.delete {
						width: 36upx;
						margin-right: 32upx;
					}
				}

			}


			.AddressManagement_list_bottom {
				font-size: 22upx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: rgba(0, 0, 0, 0.84);
				line-height: 32upx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				margin-top: 10upx;
			}
		}


		.AddressManagement_button {
			position: fixed;
			bottom: 38upx;
			left: 50%;
			transform: translate(-50%);
			width: 488upx;
			height: 82upx;
			background: #FFBE53;
			border-radius: 42upx;
			font-size: 34upx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: rgba(255, 255, 255, 0.84);
			line-height: 48upx;
			display: flex;
			align-items: center;
			justify-content: center;

			&::after {
				content: '';
				display: none;

			}
		}
	}
</style>
